<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
      <!-- <el-form-item label="台账月份" prop="billMonth">
        <el-date-picker v-model="queryParams.billMonth" type="month" placeholder="选择月" value-format="yyyy-MM" :clearable="false"> </el-date-picker>
      </el-form-item> -->

      <el-form-item label="台账月份" prop="billMonth">
        <el-date-picker v-model="queryParams.billMonth" @change="handleQuery" type="monthrange" align="right" value-format="yyyy-MM" unlink-panels range-separator="至" start-placeholder="开始月份" end-placeholder="结束月份" :picker-options="pickerOptions"> </el-date-picker>
      </el-form-item>
      <el-form-item label="属地" prop="deptId" class="deptIdClass">
        <div><treeselect v-model="queryParams.deptId" :options="deptOptions" :show-count="true" :default-expand-level="1" placeholder="请选择属地" @input="handleQuery" /></div>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="info" plain icon="el-icon-sort" size="mini" @click="toggleExpandAll">展开/折叠</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['report:export']"
          >导出</el-button
        >
      </el-col> -->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-if="refreshTable" :data="deptList" show-summary border row-key="index" :default-expand-all="isExpandAll" highlight-current-row highlight-selection-row stripe :max-height="tableHeight" style="width: 100%" :default-sort="{ prop: 'int', order: 'descending' }" :tree-props="{ children: 'children', hasChildren: 'hasChildren' }">
      <el-table-column prop="deptName" label="属地"></el-table-column>
      <el-table-column prop="wait" label="待认领件" sortable> </el-table-column>
      <el-table-column prop="finished" label="认领成功" sortable> </el-table-column>
      <el-table-column prop="dispatch" label="调度成功" sortable> </el-table-column>
      <el-table-column prop="back" label="退回件" sortable> </el-table-column>
      <el-table-column prop="imp" label="共计" sortable> </el-table-column>
    </el-table>
  </div>
</template>

<script>
import { deptTreeSelect } from "@/api/system/dept";
import { getSummary, getGroupByUser } from "@/api/hades/report/report";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "Dept",
  dicts: ["sys_normal_disable", "biz_area_owner"],
  components: {
    Treeselect,
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 表格树数据
      deptList: [],
      // 部门树选项
      deptOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否展开，默认全部展开
      isExpandAll: false,
      // 重新渲染表格状态
      refreshTable: true,
      // 查询参数
      queryParams: {
        deptName: undefined,
        deptId: null,
        billMonth: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        parentId: [{ required: true, message: "上级部门不能为空", trigger: "blur" }],
        deptName: [{ required: true, message: "部门名称不能为空", trigger: "blur" }],
      },
      groupList: [],
      groupDisplay: false,
      pickerOptions: {
        shortcuts: [
          {
            text: "本月",
            onClick(picker) {
              picker.$emit("pick", [new Date(), new Date()]);
            },
          },
          {
            text: "今年至今",
            onClick(picker) {
              const end = new Date();
              const start = new Date(new Date().getFullYear(), 0);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近六个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setMonth(start.getMonth() - 6);
              picker.$emit("pick", [start, end]);
            },
          },
        ],
      },
    };
  },
  mounted() {
    this.init();
  },
  computed: {
    tableHeight() {
      return window.innerHeight - 250;
    },
  },
  created() {
    this.init();
    this.getList();
    this.getDeptTree();
  },
  methods: {
    init() {
      const now = new Date();
      let year = now.getFullYear();
      let month = now.getMonth() + 1;
      let m = month < 10 ? "0" + month : month;
      let endMonth = `${year}-${m}`;
      let beginMonth = `${year}-01`;

      this.queryParams.billMonth = [beginMonth, endMonth];
    },
    // calcBillMonth() {
    //   let date = new Date();
    //   let year = date.getFullYear();
    //   let month = date.getMonth();
    //   let m = month < 10 ? "0" + month : month;
    //   this.queryParams.billMonth = `${year}-${m}`;
    // },
    handleCurrentChange(val) {
      console.log("333", val, this.queryParams.billMonth);
      this.currentRow = val;
      this.queryParams.deptId = val.deptId;
      this.groupDisplay = true;
      getGroupByUser(this.queryParams).then((response) => {
        console.log("aaa", response);
        this.groupList = response.data;
      });
    },
    /** 查询部门下拉树结构 */
    getDeptTree() {
      deptTreeSelect().then((response) => {
        this.deptOptions = response.data;
      });
    },
    /** 查询部门列表 */
    getList() {
      let params = {
        beginDate: this.queryParams.billMonth[0],
        endDate: this.queryParams.billMonth[1],
        deptId: this.queryParams.deptId,
      };
      this.loading = true;
      getSummary(params).then((response) => {
        this.deptList = response.data;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        deptId: undefined,
        parentId: undefined,
        deptName: undefined,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.reset();
      this.handleQuery();
    },
    /** 展开/折叠操作 */
    toggleExpandAll() {
      this.refreshTable = false;
      this.isExpandAll = !this.isExpandAll;
      this.$nextTick(() => {
        this.refreshTable = true;
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "/letter/report/claim/export",
        {
          ...this.queryParams,
        },
        `report_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
